package com.example.demo.dao.system;

import com.example.demo.model.system.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoleDAO {

//    一对多user
    @Result(column = "role_id",property = "userList" ,many = @Many(select = "com.example.demo.dao.system.UserDAO.selectByRoleId"))
    @Select("select * from role where role_id=#{roleId}")
    Role selectByRoleId(Integer roleId);


//    一对一
    @Select("select * from role where role_id=#{roleId}")
    List<Role> getByRoleId(Integer roleId);

//    查询全部
    @Select("select * from role")
    List<Role> selectAll();

//    添加
    @Insert("insert into role(role_name,`desc`)values(#{role_name},#{desc})")
    Integer addRole(Role role);

    //    删除
    @Delete("delete from role where role_id=#{role_id}")
    Integer deleteById(Integer role_id);

    //    修改
    @Update("update role set role_name=#{role_name},`desc`=#{desc} where role_id=#{role_id} ")
    Integer updateById(Role role);

    //    模糊查询
    @Select("<script>"+
            "select * from role " +
            "<where>"+
            "<if test=\"role_name !=null and role_name != ''\">"+
            "and role_name like CONCAT('%',#{role_name},'%')"+
            "</if>"+
            "<if test=\" desc !=null and desc != ''\">"+
            "and `desc` like CONCAT('%',#{desc},'%')"+
            "</if>"+
            "</where>"+
            "</script>")
    public List<Role> findRole(String  role_name,String desc) ;

}